#include <Metro.h>

#define hiPowerPin 10
#define ledPin 13
#define sensorPin A0

#define LOW_POWER_BAD_SIGNAL 0
#define LOW_POWER_GOOD_SIGNAL 1
#define LOW_POWER_SET_30S_DELAY 2
#define LOW_POWER_IS_30S_DELAY_OVER 3
#define LOW_POWER_PTT_OFF 4
#define LOW_POWER_SET_FIRST_PTT_DELAY 5
#define LOW_POWER_IS_FIRST_PTT_DELAY_OVER 6
#define LOW_POWER_AMP_ON 7
#define LOW_POWER_SET_SECOND_PTT_DELAY 8
#define LOW_POWER_IS_SECOND_PTT_DELAY_OVER 9
#define LOW_POWER_PTT_ON 10
#define HIGH_POWER_GOOD_SIGNAL 11
#define HIGH_POWER_BAD_SIGNAL 12
#define HIGH_POWER_PTT_OFF 13
#define HIGH_POWER_SET_FIRST_PTT_DELAY 14
#define HIGH_POWER_AMP_OFF 15
#define HIGH_POWER_SET_SECOND_PTT_DELAY 16
#define HIGH_POWER_PTT_ON 17

int sensorValue = 0;
Metro pttMetro = Metro(250);
Metro powerDelayMetro = Metro(30000);
boolean hiPowerMode = false;
uint8_t state = LOW_POWER_BAD_SIGNAL;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(hiPowerPin, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:

if((hiPowerMode == false) && (digitalRead(hiPowerPin) == LOW))
{
  switch(state)
  {

    case LOW_POWER_SET_30S_DELAY:
      powerDelayMetro.reset();
      Serial.println("Started 30 second low to high power timer");
      state = LOW_POWER_IS_30S_DELAY_OVER;
    break;
  
    case LOW_POWER_IS_30S_DELAY_OVER:
      if(powerDelayMetro.check())
        {
          Serial.println("30 second low to high timer expired");
          state = LOW_POWER_PTT_OFF;
        }
    break;  
    
    case LOW_POWER_PTT_OFF:
        Serial.println("PTT Toggled Off, started first PTT timer"); 
        state = LOW_POWER_SET_FIRST_PTT_DELAY;
    break; 

    case LOW_POWER_SET_FIRST_PTT_DELAY:
        pttMetro.reset();
        Serial.println("Set first low-high PTT delay");
        state = LOW_POWER_IS_FIRST_PTT_DELAY_OVER;
    break;
    
    case LOW_POWER_IS_FIRST_PTT_DELAY_OVER:
        if(pttMetro.check())
        {
          Serial.println("First low-high PTT delay over");
          state = LOW_POWER_AMP_ON;
        }
        
    case LOW_POWER_AMP_ON:
      Serial.println("Switched AMP on");
      state = LOW_POWER_SET_SECOND_PTT_DELAY;
    break;
    
    case LOW_POWER_SET_SECOND_PTT_DELAY:
      pttMetro.reset();
      Serial.println("Set second low-high PTT delay");
    break;
  
    case 4:
      if(pttMetro.check())
      { 
        digitalWrite(ledPin, HIGH);
        hiPowerMode = true;
        lowToHiCounter = 0;
        Serial.println("PTT Toggled On - High Power Mode");
      }
    break;
    
    default:  
      lowToHiCounter = 1;
  }
}

else if((hiPowerMode == true) && (digitalRead(hiPowerPin) == HIGH))
{
  switch(hiToLowCounter)
  {

    case 1:
      pttMetro.reset();
      Serial.println("PTT Toggled Off, started first PTT timer"); 
      hiToLowCounter = 2;
      break; 

    case 2:
      if(pttMetro.check())
      {
        pttMetro.reset();
        Serial.println("Switched to low power, started second PTT timer");
        hiToLowCounter = 3;
      }
    break;
  
    case 3:
      if(pttMetro.check())
      { 
        digitalWrite(ledPin, LOW);
        hiPowerMode = false;
        hiToLowCounter = 0;
        Serial.println("PTT Toggled On - Low Power Mode");
      }
    break;
    
    default:  
      hiToLowCounter = 1;
  }
}

}
